import {
  Catch,
  ExceptionFilter,
  ArgumentsHost,
  HttpException,
} from '@nestjs/common'
import { Request, Response } from 'express'

@Catch(HttpException)
export class HttpFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp()

    const req = ctx.getRequest<Request>()
    const res = ctx.getResponse<Response>()
    const next = ctx.getNext()

    const status = exception.getStatus()
    res.status(status).json({
      success: false,
      time: new Date(),
      data: exception,
      status,
      path:req.url
    })
  }
}
